#include "user_manage.h" //

void user_Vector_Upd_switch(void *dast, size_t n)
{

    elem_type *p = (elem_type *)dast;

    elem_type *data = (elem_type *)calloc(1, sizeof(elem_type));
    printf("\n请输入新的%s：", user_gfind_v[n - 1]);

    scanf(user_gscanf_matrl[n - 1], data); // 数据放入堆区空间

    getchar();

    switch (n)
    {
    case 1:
        strcpy(p->_USER.ID, (char *)data);
        break;

    case 2:
        sprintf(p->_USER.name, "%s", (char *)data);
        break;

    case 3:
        sprintf(p->_USER.phone, "%s", (char *)data);

        break;

    case 4:
        sprintf(p->_USER.ID_card, "%s", (char *)data);

        break;
    case 5:
        sprintf(p->_USER.sex, "%s", (char *)data);

        break;

    case 6:
        sprintf(p->_USER.md5_passwd, "%s", (char *)data);

        break;

    default:
        break;
    }

    free(data);
}

// 管理员才能使用
void user_Vector_Upd(vector v)
{
    int i = 0, j = 0;
    char YN_Del, c;
    char temp_ID[30] = "";
    elem_type **temp = NULL, **data_temp = NULL; // 二重指针存储是否需要修改的文件，批量删除测试

    printf("\n---------修改-----------\n");
    while (1)
    {
        data_temp = (elem_type **)realloc(data_temp, sizeof(elem_type *) * (j + 1));
        temp = (elem_type **)realloc(temp, sizeof(elem_type *) * (j + 1));
        data_temp[j] = (elem_type *)malloc(sizeof(elem_type));

        printf("请输入用户%s", user_gfind_v[0]);
        printf("\n:>");
        scanf("%s", temp_ID);
        YN_Del = getchar();

        if (NULL != (temp[j] = user_find_data(v, temp_ID, user_func_array[0], 1)))
        {
            i = 0;
            while (i < j)
            {
                if (temp[j] == temp[i])
                {
                    temp[j] = data_temp[i];
                }
                i++;
            }
            *data_temp[j] = *temp[j];
            while (1)
            {
                system("clear");
                printf("该用户信息为:\n");

                user_show_inf(data_temp[j]);

                for (i = 0; i < PROJECT_NUM; i++)
                {
                    printf("\n%d.修改%s", i + 1, user_gfind_v[i]);
                }

                printf("\n0.修改完毕\n");
                printf("\n退出(Q)\n");

                printf("\n:>");
                YN_Del = getchar();
                while ('\n' != YN_Del && '\n' != (c = getchar())) // 一个回车也会进入下一循环
                    ;

                if ('0' == YN_Del)
                {
                    break;
                }

                if ('Q' == YN_Del || 'q' == YN_Del)
                {
                    temp[j] = NULL;
                    free(data_temp[j]);
                    return;
                }

                if ('1' > YN_Del || (PROJECT_NUM + '0') < YN_Del) // 如果不是这个选项区间就重新
                {
                    continue;
                }

                user_Vector_Upd_switch(data_temp[j], YN_Del - '0'); // 进入选择输入
            }
            j++;
        }
        else
        {
            printf("\nno find\n");
        }

        printf("\n是否继续？(Y/N)");

        YN_Del = getchar();
        while ('\n' != YN_Del && '\n' != (c = getchar())) // 一个回车也会进入下一循环
            ;

        if (YN_Del == 'N' || YN_Del == 'n')
            break;
    }

    printf("是否保存操作？(Y/N)");

    YN_Del = getchar();
    while ('\n' != YN_Del && '\n' != (c = getchar())) // 一个回车也会进入下一循环
        ;

    if (YN_Del == 'Y' || YN_Del == 'y')
    {
        while (j--) // 连续更改
        {
            *(temp[j]) = *(data_temp[j]);
        }
        printf("更新数据中\n");
        user_Update_data(v); // 保存并备份
        // sleep(1);
        printf("更新成功\n");
    }
    else
    {
        printf("未能保存\n");

        // sleep(1);
    }

    //
    // printf("更新备份数据");

    return;
}
